package org.langqiao.util.uniqueno;

import java.util.Random;

import org.apache.commons.lang3.time.DateFormatUtils;

/**
 * 发送短信相关的工具类
 * @author yangmingyang
 * @date 2017年12月22日
 */
public class SmsUtil {
	/**
	 * 获取固定几位随机数
	 * @param charCount
	 * @return
	 */
	public static String getRandNum(int charCount) {
        String charValue = "";
        for (int i = 0; i < charCount; i++) {
            char c = (char) (randomInt(0, 10) + '0');
            charValue += String.valueOf(c);
        }
        return charValue;
    }

    public static int randomInt(int from, int to) {
        Random r = new Random();
        return from + r.nextInt(to - from);
    }
    
    /**
     * 获取发送短信的交易 id，长度需要控制在 20 位以内， 不能重复
     * @return
     */
    public static String getSmsTransId() {
    	long currentTime = System.currentTimeMillis();
    	Random random = new Random();
		String randomNum = String.valueOf(random.nextInt(1000));
		String result = DateFormatUtils.format(currentTime, "yyyyMMddHHmmssSSS") + randomNum;
		return result;
    }
    
    public static void main(String[] args) {
    	for(int i=0;i<100;i++){
    		/*String verifyCode = smsUtil.getRandNum(6);
    		System.out.println("短信验证码：" + verifyCode);*/
    		String transId = SmsUtil.getSmsTransId();
    		System.out.println("交易id：" + transId);
    	}
	}
}
